import request, {downFile} from '@/utils/request'

// 创建项目
export function addProject(data) {
  return request({
    url: '/project',
    method: 'post',
    data
  })
}
// 获取项目详情
export function getProjectDetail(id) {
  return request({
    url: `/project/detail/${id}`,
    method: 'get'
  })
}

// 获取项目列表
export function getProjectList(params) {
  return request({
    url: '/project',
    method: 'get',
    params
  })
}

// 修改项目名称
export function updateProjectInfo(data) {
  return request({
    url: '/project',
    method: 'put',
    data
  })
}

// 删除项目
export function deleteProject(id) {
  return request({
    url: '/project',
    method: 'delete',
    params: { id }
  })
}

// 获取项目中文件的统计信息
export function getProjectFileStats(projectId) {
  return request({
    url: `/project/fileStats/${projectId}`,
    method: 'get'
  })
}

export function getProjectFileInfo(projectId) {
  return request({
    url: `/file/project-file/${projectId}`,
    method: 'get'
  })
}

// 创建项目
export function addRequire(data) {
  return request({
    url: '/project',
    method: 'post',
    data
  })
}
// 获取项目详情
export function getRequireDetail(id) {
  return request({
    url: `/project/detail/${id}`,
    method: 'get'
  })
}

// 获取项目列表
export function getRequireList(params) {
  return request({
    url: '/int-tool-data/list-requirement',
    method: 'get',
    params
  })
}

// 修改项目名称
export function updateRequireInfo(data) {
  return request({
    url: '/project',
    method: 'put',
    data
  })
}
// 通用接口
export function getAllList(params) {
  return request({
    url: '/int-tool-data/list',
    method: 'get',
    params
  })
}
export function getProDetail(id) {
  return request({
    url: `/project/detail/${id}`,
    method: 'get'
  })
}
// 通用接口
export function setInfo(data) {
  return request({
    url: '/int-tool-data',
    method: 'put',
    data
  })
}
// 文件上传通用接口
export function uploadFile(projKey, fileType, files) {
  const formData = new FormData();
  files.forEach(file => formData.append('files', file));
  formData.append('projKey', projKey);
  formData.append('fileType', fileType);

  return request({
    url: '/file/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 文件下载通用接口
export function downloadFile(fileVersionId) {
  return request({
    url: `/file-download/file-version/${fileVersionId}`,
    method: 'get',
    responseType: 'blob',
    headers: {needHeader: true}
  }).then(downFile)
}

// 获取数据版本信息
export function getDataVersions(dataKeys) {
  return request({
    url: '/file/data-version',
    method: 'get',
    params: {
      dataKeys: dataKeys.join(',')
    }
  })
}

// 高层需求模型上传接口
export function uploadHlrModel(projKey, files) {
  return uploadFile(projKey, 'HLR_MODEL', files);
}
// 一致性检查报告上传接口
export function uploadHlrConsistency(projKey, files) {
  return uploadFile(projKey, 'HLR_CONSISTENCY', files);
}
// 架构模型上传接口
export function uploadArchiModel(projKey, files) {
  return uploadFile(projKey, 'ARCHI_MODEL', files);
}

//

export function downloadHlrModel(projKey, filePath, versionHash) {
  return downloadFile(projKey, 'HLR_MODEL', filePath, versionHash);
}

export function downloadControlCode(projKey, filePath, versionHash) {
  return downloadFile(projKey, 'CONTROL_CODE', filePath, versionHash);
}

export function downloadDisplayCode(projKey, filePath, versionHash) {
  return downloadFile(projKey, 'DISPLAY_CODE', filePath, versionHash);
}

export function downloadImplCode(projKey, filePath, versionHash) {
  return downloadFile(projKey, 'IMPL_CODE', filePath, versionHash);
}
